package view.dic;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;

import entiey.Dictionary;
import entiey.TypeId;
import service.DictionaryService;
import service.impl.DictionaryServiceImpl;
import util.CastUtil;

/**
 * 字典页面数据操作
 * @author 秦洪涛
 *2020年8月10日下午10:11:24
 */
public class V_Dictionary extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_Dictionary frame = new V_Dictionary();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_Dictionary() {
		setTitle("数据字典");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 374, 361);
		contentPane = new JPanel();
		contentPane.setToolTipText("数据字典");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, BorderLayout.CENTER);
		/**
		 * 1.tab标题
		 * 2.tab图标
		 * 3.添加到tab中的组件
		 * 4.鼠标悬停到tab选项卡上的提示
		 */
		JPanel p_combotype = new JPanel();
		tabbedPane.addTab("套餐设置", null, p_combotype, "套餐设置");
		p_combotype.setLayout(new BorderLayout(0, 0));

		tree_comb = new JTree();
		tree_comb.setToolTipText("数据字典");
		p_combotype.add(tree_comb, BorderLayout.CENTER);

		JPanel p_foodtype = new JPanel();
		tabbedPane.addTab("菜单类别设置", null, p_foodtype, null);
		p_foodtype.setLayout(new BorderLayout(0, 0));

		tree_menu = new JTree();
		
		p_foodtype.add(tree_menu, BorderLayout.CENTER);
		

		JPanel p_viptype = new JPanel();
		tabbedPane.addTab("会员级别设置", null, p_viptype, null);
		p_viptype.setLayout(new BorderLayout(0, 0));

		tree_vip = new JTree();
		
		p_viptype.add(tree_vip, BorderLayout.CENTER);



		JPanel p_btns_1 = new JPanel();
		contentPane.add(p_btns_1, BorderLayout.SOUTH);

		JButton btn_new = new JButton("新建");
		//新建按钮
		btn_new.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//根基获取的选项卡判断添加的值内容
				//获取选项卡下表  0
				int tabIndex=tabbedPane.getSelectedIndex();
				Dictionary d=new Dictionary(); 
				String title="";
				switch (tabIndex) {
				case 0:
					title="套餐类别";
					d.setTypeid(TypeId.套餐类别);
					break;
				case 1:
					title="菜单类别";
					d.setTypeid(TypeId.菜单类别);
					break;
				case 2:
					title="会员类别";
					d.setTypeid(TypeId.会员级别);
					break;
				}
				String value= JOptionPane.showInputDialog(V_Dictionary.this,"请输入"+title+"的名称",title,JOptionPane.PLAIN_MESSAGE);
				if (value!=null && !"".equals(value)) {
					d.setName(value);
					showMessage(dService.saveDictionary(d), "添加");
				}

			}
		});
		p_btns_1.add(btn_new);

		JButton btn_reset = new JButton("重命名");
		//重命名
		btn_reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JTree tree=null;
				//获取选项卡下表  0套餐1菜单
				int tabIndex=tabbedPane.getSelectedIndex();
				String title="";
				switch (tabIndex) {
				case 0:
					title="套餐类别";
					tree=tree_comb;
					break;
				case 1:
					title="菜单类别";
					tree=tree_menu;
					break;
				case 2:
					title="会员级别";
					tree=tree_vip;
					break;
				}
				//获取选中的节点
				String selectValue=CastUtil.castString(tree.getSelectionPath().getLastPathComponent());
				Object value=JOptionPane.showInputDialog(V_Dictionary.this, "请输入"+title+"名称", title, JOptionPane.PLAIN_MESSAGE, null, null, selectValue);
				if (value!=null && !"".equals(value)) {
					DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode) tree.getSelectionPath().getLastPathComponent();
					//获取添加到节点的对象，节点存储的是什么，获取的就是什么
					Dictionary d=(Dictionary) dmtn.getUserObject();
					d.setName(CastUtil.castString(value));
					showMessage(dService.updateDictionaryById(d), "修改");
				}else {
					showMessage(false, "清选择修改的节点");
				}
			}
		});
		p_btns_1.add(btn_reset);

		JButton btn_delete = new JButton("删除");
		//删除按钮
		btn_delete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				JTree tree=null;
				//获取选项卡下表
				int tabIndex=tabbedPane.getSelectedIndex();
				switch (tabIndex) {
				case 0:
					tree=tree_comb;
					break;
				case 1:
					tree=tree_menu;
					break;
				case 2:
					tree=tree_vip;
					break;

				}
				////获取树节点的数据模型
				DefaultTreeModel dtm=(DefaultTreeModel) tree.getModel();
				//通过节点的数据模型删除选中的节点对象
				TreePath selectPath= tree.getSelectionPath();
				if (selectPath!=null) {
					//获取删除节点的最后一个节点
					MutableTreeNode mtn=(MutableTreeNode) selectPath.getLastPathComponent();
					//询问是否删除  JOptionPane返回值是int  JOptionPane.OK_CANCEL_OPTION只有确定或取消
					int n=JOptionPane.showConfirmDialog(V_Dictionary.this, "确定删除?","删除",JOptionPane.OK_CANCEL_OPTION);
					if (n==0) {
						//根据编号删除
						DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode) mtn;
						//获取添加到节点的对象，节点存储的是什么，获取的就是什么
						Dictionary d=(Dictionary) dmtn.getUserObject();
						showMessage(dService.deleteDictionaryById(d.getId()), "删除");
						//删除节点
						dtm.removeNodeFromParent(mtn);
					}
				}else {
					//未选中节点
					JOptionPane.showMessageDialog(V_Dictionary.this, "请先选择删除的节点");
				}
			}

		});
		p_btns_1.add(btn_delete);
		//初始化
		initTree();

	}

	/**
	 * 初始化三棵树节点对象
	 */
	public void initTree() {
		initTypeTree();
		initUnitTree();
		initVipTree();

	}
	private JTree tree_vip;
	private JTree tree_menu;
	private JTree tree_comb;
	DictionaryService dService=new DictionaryServiceImpl();
	/**
	 * 初始化套餐的方法
	 * 
	 */
	public void initUnitTree() {

		List<Dictionary> combList =dService.findDictionaryByTypeID(TypeId.套餐类别);
		DefaultMutableTreeNode dmtn=new DefaultMutableTreeNode("套餐节点");
		for (Dictionary comb : combList) {
			dmtn.add(new DefaultMutableTreeNode(comb));
		}
		DefaultTreeModel dtm=new DefaultTreeModel(dmtn);
		tree_comb.setModel(dtm);
	}
	/**
	 * 初始化vio的方法
	 * 
	 */
	public void initVipTree() {

		List<Dictionary> vipList =dService.findDictionaryByTypeID(TypeId.会员级别);
		DefaultMutableTreeNode dmtn=new DefaultMutableTreeNode("会员设置");
		for (Dictionary vip : vipList) {
			dmtn.add(new DefaultMutableTreeNode(vip));
		}
		DefaultTreeModel dtm=new DefaultTreeModel(dmtn);
		tree_vip.setModel(dtm);
	}
	/**
	 * 初始化菜单
	 */
	public void initTypeTree() {

		List<Dictionary> menuList=dService.findDictionaryByTypeID(TypeId.菜单类别);
		DefaultMutableTreeNode dmtn=new DefaultMutableTreeNode("菜单节点");
		for (Dictionary menu : menuList) {
			dmtn.add(new DefaultMutableTreeNode(menu));
		}
		DefaultTreeModel dtm=new DefaultTreeModel(dmtn);
		tree_menu.setModel(dtm);
	}



	/**
	 * 消息提示
	 */
	public void showMessage(boolean bool,String msg) {
		if (bool) {
			JOptionPane.showMessageDialog(V_Dictionary.this, msg+"成功",msg,JOptionPane.INFORMATION_MESSAGE);
			initTree();//成功后调用初始化数
		}else {
			JOptionPane.showMessageDialog(V_Dictionary.this, msg+"失败",msg,JOptionPane.ERROR_MESSAGE);
		}
	}
}
